package l2ft.gameserver.handler.voicecommands.impl;


import l2ft.gameserver.Config;
import l2ft.gameserver.data.htm.HtmCache;
import l2ft.gameserver.handler.voicecommands.IVoicedCommandHandler;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.entity.poll.Poll;
import l2ft.gameserver.model.entity.poll.PollAnswer;
import l2ft.gameserver.model.entity.poll.PollEngine;
import l2ft.gameserver.network.l2.s2c.NpcHtmlMessage;
import l2ft.gameserver.scripts.Functions;

public class PollCommand extends Functions implements IVoicedCommandHandler
{
	private String[] _commandList = new String[] { "poll", "poll_change", "poll_vote" };
	
	public boolean useVoicedCommand(String command, Player activeChar, String args)
	{
		if(!Config.ENABLE_POLL_SYSTEM)
			return false;
		
		String fileName = "";

		Poll activePoll = PollEngine.getInstance().getActivePoll();
		System.out.println("activeChar vote:"+activeChar.getPollVote());
		
		if(activePoll == null)
		{
			fileName = "pollEmpty.htm";
		}
		else if(command.equals("poll_vote"))
		{
			fileName = "pollResults.htm";
			int answerId = Integer.parseInt(args.trim());
			activePoll.addVote(activeChar, answerId);
		}
		else if(!command.equals("poll_change") && activeChar.getPollVote() >= 0)
		{
			System.out.println("results");
			fileName = "pollResults.htm";
		}
		else
		{
			fileName = "pollVote.htm";
		}
		
		String html = HtmCache.getInstance().getNotNull("command/" + fileName, activeChar);
		
		if(html.contains("%question%"))
			html = html.replace("%question%", activePoll.getQuestion());
		
		if(html.contains("%endTime%"))
			html = html.replace("%endTime%", activePoll.getPollEndDate());
		
		if(html.contains("%answers%"))
			html = fillAnswers(html, activeChar);
		
		if(html.contains("%results%"))
			html = fillResults(html, activeChar);
		
		NpcHtmlMessage msg = new NpcHtmlMessage(0);
		msg.setHtml(html);
		activeChar.sendPacket(msg);
		return true;
	}

	private String fillAnswers(String html, Player activeChar)
	{
		PollAnswer[] answers = PollEngine.getInstance().getPoll().getAnswers();
		String results = "<table width=280><tr><td>";
		
		
		for(int i = 0;i<answers.length;i++)
		{
			PollAnswer answer = answers[i];
			results += "<table width=280 bgcolor="+(getColor(i))+"><tr><td width=200>";
			results += answer.getAnswer();
			results += "</td><td width=80>";
			results += getButton("Vote!", "user_poll_vote "+answer.getId());
			results += "</td></tr></table>";
		}
		
		results += "</td></tr></table>";
		
		html = html.replace("%answers%", results);
		return html;
	}

	private String fillResults(String html, Player activeChar)
	{
		Poll currentPoll = PollEngine.getInstance().getPoll();
		int answersCount = currentPoll.getAnswers().length;
		PollAnswer[] answersToSort = new PollAnswer[answersCount];
		
		for(int i = 0;i<answersCount;i++)
		{
			answersToSort[i] = currentPoll.getAnswers()[i];
		}
		answersToSort = PollEngine.getInstance().sortAnswers(answersToSort);
		
		String results = "<table width=280><tr><td>";
		
		for(int i = 0;i<answersToSort.length;i++)
		{
			PollAnswer answer = answersToSort[i];
			results += "<table width=280 bgcolor=";
			results += (activeChar.getPollVote() == answer.getId() ? "7d805a" : getColor(i));
			results += "><tr><td width=200>";
			results += answer.getAnswer();
			results += "</td><td width=80><center>";
			results += PollEngine.getInstance().getAnswerProcentage(answer)+"%";
			results += "</center></td></tr></table>";
		}
		
		results += "</td></tr></table>";
		
		html = html.replace("%results%", results);
		return html;
	}
	
	private static String getColor(int index)
	{
		return (index % 2 == 0 ? "313a37" : "3a3a31");
	}
	
	private static String getButton(String buttonText, String bypass)
	{
		return "<button value=\""+buttonText+"\" action=\"bypass -h "+bypass+"\" width="+(buttonText.length() > 18 ? 170 : 80)+" height=30 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\">";
	}

	public String[] getVoicedCommandList()
	{
		return _commandList;
	}
}